<?php
class getMail extends ActionHandler 
{
	function handleRequest($jsonData)
	{
		$data = $jsonData['params'];
		$toId = $data['toId'];
		$sql = "select a.id, a.fromId, a.fromName, a.message, b.mailId, b.objectIndex, b.objectNum from ".$this->tables['mail_profile']." a left join ".$this->tables['gift_profile']." b on a.id = b.mailId where toId = ".$toId;
		$result = $this->db->query($sql);
		$return = array();
		$mail = array();
		$i = 0;
		while($arr = $this->db->fetchArray($result))
		{
			if ($return[$arr['id']] == null)
			{
				$mail = array();
				$mail['id'] = $arr['id'];
				$mail['toId'] = $toId;
				$mail['fromId'] = $arr['fromId'];
				$mail['fromName'] = $arr['fromName'];
				$mail['message'] = $arr['message'];
			}
			else
			{
				$mail = $return[$arr['id']];
			}
			if ($arr['mailId'] != null)
			{
				$gift = array();
				$gift['objectIndex'] = $arr['objectIndex'];
				$gift['objectNum'] = $arr['objectNum'];
				$mail['giftArray'][$i] = $gift;
			}
			$return[$arr['id']] = $mail;
			$i++;
		}
		return $return;
	}
}
?>
